#include <QtGui/QApplication>
#include <QTextCodec>
#include <QSettings>
#include <QTranslator>
#include <QDebug>
#include <QObject>

#include "definitions.h"
#include "formmain.h"
#include "singleapplication.h"
#ifdef HAVE_DBUS
#include "dbusadaptor.h"
#endif

int main(int argc, char *argv[]) {
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

#ifdef Q_WS_MAC
    QSettings::setDefaultFormat(QSettings::IniFormat);
#endif

    // obtain username for local server key/name
    // The logic behinf it is to have unique (per session)
    // ID for rssguard to ensure only one app is started
    // Let's suppose it's not possible to start 2 sessions
    // for one user on Windows (username only). But check
    // the DISPLAY on UN*X where it's possible. Examples:
    //   mac: pvanek_/tmp/launch-sXYeRy/org.macosforge.xquartz:0
    //   linux: pvanek_:0
    QString uname;
#ifdef Q_OS_UNIX
    // mac, linux, and the other unices
    uname = qgetenv("USER");
    if (!uname.isEmpty())
        uname += "_" + qgetenv("DISPLAY");
#endif
#ifdef Q_WS_WIN
    // TODO/FIXME: I don't have any Windows machine available. Please verify. Thanks, PV.
    uname = qgetenv("USERNAME");
#endif
    if (uname.isEmpty()) {
        qDebug() << "cannot get uname. Using default";
        uname = "RSSGuard";
    }

    SingleApplication rssApp(argc, argv, uname);

    if (rssApp.isRunning()) {
		return 0;
	}

    // these settings needs to be set before any QSettings object
    rssApp.setApplicationName("RSS Guard");
    rssApp.setApplicationVersion("1.0.6");
    rssApp.setOrganizationName("Martin Rotter");
    rssApp.setOrganizationDomain("http://code.google.com/p/rss-guard/");

    QSettings *sett;
    QString defaultPath = APP_PATH_BASE + QDir::separator() + APP_PATH_CONFIG;

    if (QFile(qApp->applicationDirPath() + QDir::separator() + APP_PATH_CONFIG).exists())
        sett = new QSettings(qApp->applicationDirPath() + QDir::separator() + APP_PATH_CONFIG, QSettings::IniFormat, NULL);
    else
        sett = new QSettings(defaultPath, QSettings::IniFormat, NULL);

    FormMain::checkSettings(sett);

    QTranslator qtTranslator;
    QString path = ":/lang/lang/";
    QString lang = sett->value("currentLanguage", "").toString();

    qtTranslator.load(lang, path);
    rssApp.installTranslator(&qtTranslator);

    qDebug() << QObject::tr("%1%2 %3 is launching.").arg(APP_ID, rssApp.applicationName(), rssApp.applicationVersion());

    FormMain rssGui;

#ifdef HAVE_DBUS
    new DBusAdaptor(&rssGui);
    // connect to D-Bus and register as an object:
    QDBusConnection::sessionBus().registerService(DBUS_SERVICE);
    QDBusConnection::sessionBus().registerObject(DBUS_PATH, &rssGui);
#endif

    if (sett->value("startMinimized", false).toBool())
        rssGui.hide();
    else
        rssGui.show();

    return rssApp.exec();
}
